package cn.com.jit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.jit.service.UserService;
import cn.com.jit.vo.User;

@Controller
public class UserController {
	
	private User user;
	
	@Autowired
	private UserService userService;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@RequestMapping("user.do")
	private String list(ModelMap map){
		
		map.addAttribute("list", userService.list());
		
		return "show.jsp";
	}
	
	@RequestMapping("inputindex.do")
	public String inputindex(){
		
		return "index.jsp";
	}
	
	@RequestMapping("add.do")
	public String add(User user){
		user.setId(((userService.list().size()+1)+"").trim());
		userService.add(user);
		
		return "redirect:user.do";
	}
	
	@RequestMapping("delete.do")
	public String delete(String id){
		userService.delete(id);
		return "redirect:user.do";
	}
	
	@RequestMapping("gotoupdate.do")
	public String gotoUpdate(String id, ModelMap map){
		user = userService.findUserById(id);
		map.addAttribute("user", user);
		return "update.jsp";
	}
	
	@RequestMapping("update.do")
	public String updateUser(User user){
		//System.out.println(user.getId());
		userService.updateUser(user);
		return "redirect:user.do";
	}

}
